package com.aries.remoteplus.view;

import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aries.remoteplus.R;
import com.aries.remoteplus.core.RPConstants;
import com.illposed.osc.OSCPortOut;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class RemotesActivity extends Activity {

	private String ipAddress;
	private String serverName;
	private int tcpPort;
	private int udpPort;
	private String passWord;
	private OSCPortOut sender;
	private Intent basicInputIntent;
	private Intent keyboardIntent;
	private Intent numPadIntent;
	private Intent navIntent;
	private Intent mediaIntent;
	private Intent powerIntent;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.remotes);
		
		Intent dataIntent = this.getIntent();
		Bundle dataBundle = dataIntent.getExtras();
		
		this.ipAddress 		= dataBundle.getString("ipAddress");
		this.tcpPort 		= dataBundle.getInt("tcpPotr");
		this.udpPort 		= dataBundle.getInt("udpPort");
		
		if (ipAddress != null)
		{
			try
			{
				sender = new OSCPortOut(InetAddress.getByName(this.ipAddress), this.tcpPort);
			} catch (SocketException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnknownHostException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		ListView remotesListView = (ListView) findViewById(R.id.remotes);
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, getData(), R.layout.list_item, 
													new String[]{"title", "hint", "icon"}, 
													new int[]{R.id.text_title, R.id.text_hint, R.id.icon});
		
		remotesListView.setAdapter(simpleAdapter);
		
		this.basicInputIntent 	= new Intent(this, BasicInputActivity.class);
		this.keyboardIntent 	= new Intent(this, KeyboardActivity.class);
		this.numPadIntent 		= new Intent(this, NumPadActivity.class);
		this.navIntent			= new Intent(this, NavigationActivity.class);
		this.mediaIntent		= new Intent(this, MediaActivity.class);
		this.powerIntent		= new Intent(this, PowerActivity.class);
		remotesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				switch ( position )
				{
					case 0:
					{
						if (sender != null)
						{
							basicInputIntent.putExtra("ipAddress", ipAddress);
							basicInputIntent.putExtra("serverName", serverName);
							basicInputIntent.putExtra("tcpPort", tcpPort);
							basicInputIntent.putExtra("udpPort", udpPort);
							basicInputIntent.putExtra("passWord", passWord);
						}
						startActivity(basicInputIntent);
						
						break;
					}
					
					case 1:
					{
						if (sender != null)
						{
							keyboardIntent.putExtra("ipAddress", ipAddress);
							keyboardIntent.putExtra("udpPort", udpPort);
						}
						startActivity(keyboardIntent);
						
						break;
					}
					
					case 2:
					{
						break;
					}
					
					case 3:
					{
						if (sender != null)
						{
							mediaIntent.putExtra("ipAddress", ipAddress);
							mediaIntent.putExtra("udpPort", udpPort);
						}
						startActivity(mediaIntent);
						
						break;
					}
					
					case 4:
					{
						if (sender != null)
						{
							navIntent.putExtra("ipAddress", ipAddress);
							navIntent.putExtra("udpPort", udpPort);
						}
						startActivity(navIntent);
						
						break;
					}
					
					case 5:
					{
						if (sender != null)
						{
							numPadIntent.putExtra("ipAddress", ipAddress);
							numPadIntent.putExtra("udpPort", udpPort);
						}
						startActivity(numPadIntent);
						
						break;
					}
					
					case 6:
					{
						if (sender != null)
						{
							powerIntent.putExtra("ipAddress", ipAddress);
							powerIntent.putExtra("udpPort", udpPort);
						}
						startActivity(powerIntent);
						
						break;
					}
					
				}
			}
		});
		
	}
	
	private List< Map<String, Object> > getData()
	{
		List< Map<String, Object> > dataList = new ArrayList< Map<String,Object> >();
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("title", RPConstants.TITLE_BASICINPUT);
		dataMap.put("hint", RPConstants.HINT_BASICINPUT);
		dataMap.put("icon", R.drawable.icon);
		dataList.add(dataMap);
		
		dataMap = new HashMap<String, Object>();
		dataMap.put("title", RPConstants.TITLE_KEYBOARD);
		dataMap.put("hint", RPConstants.HINT_KEYBOARD);
		dataMap.put("icon", R.drawable.icon);
		dataList.add(dataMap);
		
		dataMap = new HashMap<String, Object>();
		dataMap.put("title", RPConstants.TITLE_FILEMANAGER);
		dataMap.put("hint", RPConstants.HINT_FILEMANAGER);
		dataMap.put("icon", R.drawable.icon);
		dataList.add(dataMap);
		
		dataMap = new HashMap<String, Object>();
		dataMap.put("title", RPConstants.TITLE_MEDIA);
		dataMap.put("hint", RPConstants.HINT_MEDIA);
		dataMap.put("icon", R.drawable.icon);
		dataList.add(dataMap);
		
		dataMap = new HashMap<String, Object>();
		dataMap.put("title", RPConstants.TITLE_NAVIGATION);
		dataMap.put("hint", RPConstants.HINT_NAVIGATION);
		dataMap.put("icon", R.drawable.icon);
		dataList.add(dataMap);
		
		dataMap = new HashMap<String, Object>();
		dataMap.put("title", RPConstants.TITLE_NUMPAD);
		dataMap.put("hint", RPConstants.HINT_NUMPAD);
		dataMap.put("icon", R.drawable.icon);
		dataList.add(dataMap);
		
		dataMap = new HashMap<String, Object>();
		dataMap.put("title", RPConstants.TITLE_POWER);
		dataMap.put("hint", RPConstants.HINT_POWER);
		dataMap.put("icon", R.drawable.icon);
		dataList.add(dataMap);
		
		return dataList;
	}
}
